package com.yf.yfmovieapp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yf.yfmovieapp.pojo.R;
import com.yf.yfmovieapp.pojo.TimeTransformer;
import com.yf.yfmovieapp.pojo.User;
import com.yf.yfmovieapp.pojo.WlUser;
import com.yf.yfmovieapp.pojo.vo.AppVersionInfo;
import com.yf.yfmovieapp.pojo.vo.FrontUserInfo;
import com.yf.yfmovieapp.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * @Author Yang_Vincent
 * @Date 2020/8/29 9:39
 */
@RestController
@Slf4j
@RequestMapping("/login")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/qiandao")
    public R singSuccess(
            @RequestParam(value = "uid",required = false)String uid
    ){
        log.info(uid);
        return R.ok("杨Sir,友情提示签到成功!");
    }

    @PostMapping("/getinfo")
    public WlUser getOk(){
        WlUser wlUser = new WlUser();

        wlUser.setUid("684");
        wlUser.setPic(null);
        wlUser.setName("");
        wlUser.setVip("1");
        wlUser.setMoney("999");
        wlUser.setRegdate("159866438");
        wlUser.setRegip("1.80.144.28");
        wlUser.setUregcode("");
        wlUser.setLock("n");
        wlUser.setOnline("8b5c277a8e1f5578ee5bc15171443ec");
        wlUser.setInv("0");
        wlUser.setNumber("0");

        return wlUser;
    }

    @GetMapping
    public R loginOkV2(
            @RequestParam("username")String username,
            @RequestParam("passwd")String password
    ){

        User user = this.userService.getUserIssTrue(username,password);

        if (user != null){
            FrontUserInfo frontUserInfo = new FrontUserInfo();
            BeanUtils.copyProperties(user, frontUserInfo);

            frontUserInfo.setTime(user.getTime().toEpochSecond(ZoneOffset.of("+8")));

            log.info("当前登录用户为:{},登录时间为:{}",username,LocalDateTime.now());

            return R.ok(frontUserInfo);
        }
        log.info("当前登录用户为:{} 登录失败,时间为:{}",username,LocalDateTime.now());
        return R.fail("用户名或密码不正确");
    }

    @GetMapping("/register")
    public R registerSuccess(
            @RequestParam("name")String username,
            @RequestParam("password")String password
    ){
        User user = new User();

        user.setTime(LocalDateTime.now().plusYears(1));
        user.setShiYong(36);
        user.setPower("3");
        user.setShare("36");
        user.setJe(3);
        user.setDs("3");
        user.setUsername(username);
        user.setPassword(password);

        this.userService.save(user);

        return R.ok("注册成功");
    }
    @GetMapping("/verify")
    public R registerAndLogn(
            @RequestParam("username")String username,
            @RequestParam("password")String password
    ){

        log.info("当前注册并登录用户为:{},登录时间为:{}",username,LocalDateTime.now());

        User user = new User();

        user.setTime(LocalDateTime.now().plusYears(1));
        user.setShiYong(36);
        user.setPower("3");
        user.setShare("36");
        user.setJe(3);
        user.setDs("3");
        user.setUsername(username);
        user.setPassword(password);

        this.userService.save(user);

        FrontUserInfo frontUserInfo = new FrontUserInfo();

        BeanUtils.copyProperties(user, frontUserInfo);

        frontUserInfo.setTime(user.getTime().toEpochSecond(ZoneOffset.of("+8")));

        return R.ok(frontUserInfo);
    }

    @GetMapping("/imei")
    public R getImeiId(
            @RequestParam("uid")String uid
    ){
        User byId = this.userService.getById(uid);
        if (byId != null){
            FrontUserInfo frontUserInfo = new FrontUserInfo();

            BeanUtils.copyProperties(byId, frontUserInfo);

            return R.ok(frontUserInfo);
        }
        return R.fail("用户效验失败");
    }
    @GetMapping("/version/info")
    public AppVersionInfo getVersionInfoOk(){

        return AppVersionInfo.infoOk();
    }
}
